package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.service.PersonalCenterService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service(interfaceClass = PersonalCenterService.class)
@Transactional
public class PersonalCenterServiceImpl implements PersonalCenterService {

    @Autowired
    private MemberDao memberDao;

    @Override
    public Member findByTelephone(String telephone) {
        Member member = memberDao.findByTelephone(telephone);
        String oldIdCard = member.getIdCard();
        if (oldIdCard != null && oldIdCard.length() > 0) {
            String newIdCard = oldIdCard.substring(0, 2) + "****************" + oldIdCard.substring(16);
            member.setIdCard(newIdCard);
        }
        return member;
    }

    /**
     * 修改个人信息
     *
     * @param member
     */
    @Override
    public void modifyPersonalCenter(Member member) {
        String idCard = member.getIdCard();
        if (idCard != null && idCard.contains("*")) {
            member.setIdCard(null);
        }
        if (idCard != null && !idCard.contains("*")) {
            String birthdayStr = idCard.substring(6, 14);
            try {
                Date birthday = DateUtils.parseString2Date(birthdayStr, "yyyyMMdd");
                member.setBirthday(birthday);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        memberDao.edit(member);
    }
}
